package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.query.ProductSkuQuery;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.vo.ProductSkuVO;
import com.spzx.product.service.ProductService;
import com.spzx.product.service.ProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("product")
@Tag(name = "商品管理模块")
public class ProductController extends BaseController {
    @Resource
    ProductService productService;
    @Resource
    ProductSkuService productSkuService;

    @InnerAuth
    @Operation(summary = "内部调用接口：查询一组sku的价格")
    @PostMapping("getSkusPrice")
    public R<Map<Long , BigDecimal>> getSkusPrice(@RequestBody List<Long> skuIds){
        return R.ok(productSkuService.getSkusPrice(skuIds));
    }
    @InnerAuth
    @Operation(summary = "内部调用接口：查询sku详情页数据")
    @GetMapping("getSkuDetails/{skuId}")
    public R<ProductSkuVO> getSkuDetails(@PathVariable("skuId")Long skuId){
        return R.ok(productSkuService.selectSkuDetails(skuId));
    }

    @InnerAuth
    @Operation(summary = "内部调用接口：条件查询sku分页数据")
    @GetMapping("listSkus")
    public R<Page<ProductSku>> listSkus(ProductSkuQuery query){
        return R.ok(productSkuService.selectListSkus(query));
    }


    @InnerAuth
    @Operation(summary = "内部调用接口：查询首页热门数据")
    @GetMapping("listIndexSkus")
    public R<List<ProductSku>> listIndexSkus(){
        return R.ok(productSkuService.selectIndexSkus());
    }


    @Operation(summary = "商品审核：通过 或者 驳回  决定商家是否可以发布商品给用户购买")
    @PutMapping("updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult updateAuditStatus(@PathVariable Long id,
                                        @PathVariable Integer auditStatus,
                                        @RequestParam(value = "auditMessage",required = false) String auditMessage){
        return toAjax(productService.update(Wrappers.lambdaUpdate(Product.class)
                .eq(Product::getId , id)
                .set(Product::getAuditStatus ,auditStatus )
                .set(Product::getAuditMessage , auditMessage)
                .set(Product::getUpdateBy , SecurityUtils.getUsername())
            )
        );
    }
    @Operation(summary = "商品上下架：上架 或者 下架   决定用户是否可以看到商品")
    @PutMapping("updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable Long id,
                                        @PathVariable Integer status){
        return toAjax(productService.updateStatus(id,status));
    }
    @Operation(summary = "条件查询商品分页列表")
    @GetMapping("list")
    public TableDataInfo list(Product product){
        Page<Product> page = productService.selectProductPage(product);
        return getDataTable(page);
    }
    @Operation(summary = "新增商品spu")
    @PostMapping
    public AjaxResult save(@Validated  @RequestBody Product product){
        return toAjax(productService.saveProduct(product));
    }
    @Operation(summary = "更新商品spu")
    @PutMapping
    public AjaxResult edit(@Validated  @RequestBody Product product){
        return toAjax(productService.updateProduct(product));
    }
    @Operation(summary = "根据id查询商品详情")
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id){
        Product product = productService.selectProductById(id);
        return success(product);
    }
    @Operation(summary = "根据id删除商品")
    @DeleteMapping("{id}")
    public AjaxResult remove(@PathVariable Long id){
        return toAjax(productService.removeProductById(id));
    }
}
